package com.tdam2011.grupo02.ui;

import java.util.LinkedList;

import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.domain.Mail;
import com.tdam2011.grupo02.data.LogsController;
import com.tdam2011.grupo02.widget.popup.ActionItem;
import com.tdam2011.grupo02.widget.popup.QuickAction;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;

public class MailsLogListFragment extends ListFragment {

	private Context context;
	private MailAdapter mailAdapter;
	private ListView mailListView;
	private QuickAction mailPopup;
	private int adapterPosition;
	private LogsController controller;
	private MailsAsyncTask mailsAsyncTask;

	private final static int POPUP_ACTION_ITEM_DELETE = 12;

	private final static String TAG = "MailsLogListFragment";

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setListShown(true);

		mailListView = getListView();

		mailAdapter = new MailAdapter();
		mailListView.setAdapter(mailAdapter);
		mailListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				adapterPosition = position;

				int firstPosition = getListView().getFirstVisiblePosition()
						- getListView().getHeaderViewsCount();
				int wantedChild = adapterPosition - firstPosition;

				View v = (View) mailListView.getChildAt(wantedChild);

				mailPopup.show(v);
				return false;
			}
		});

		controller = new LogsController(context);
		createMailPopup();
		mailsAsyncTask = new MailsAsyncTask();
		mailsAsyncTask.execute();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
	inflater.inflate(R.layout.log_mails,
				container);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	public void createMailPopup() {
		ActionItem eliminarItem = new ActionItem(POPUP_ACTION_ITEM_DELETE,
				null, getResources().getDrawable(
						android.R.drawable.ic_menu_delete));

		mailPopup = new QuickAction(context);

		mailPopup.addActionItem(eliminarItem);

		mailPopup
				.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					@Override
					public void onItemClick(QuickAction quickAction, int pos,
							int actionId) {

						if (actionId == POPUP_ACTION_ITEM_DELETE)
							((Activity) context)
									.showDialog(POPUP_ACTION_ITEM_DELETE);
					}
				});

		mailPopup.setOnDismissListener(new QuickAction.OnDismissListener() {
			@Override
			public void onDismiss() {
			}
		});

	}

	private class MailsAsyncTask extends
			AsyncTask<String, String, LinkedList<Mail>> {

		@Override
		protected LinkedList<Mail> doInBackground(String... arg0) {
			return getMailsData();
		}

		@Override
		protected void onPostExecute(LinkedList<Mail> result) {
			mailAdapter.add(result);
			mailAdapter.notifyDataSetChanged();
			Log.d(TAG,
					"onPostExecute() - adapter.getCount() = "
							+ mailAdapter.getCount());
			super.onPostExecute(result);
		}
	}

	private LinkedList<Mail> getMailsData() {
		ContactDataInterface contact = (HistorialLogFragmentActivity) context;
		String nombreContacto= contact.onDataSelected()[1];
		
		return controller.getAllMails(nombreContacto);
	}

	class MailAdapter extends BaseAdapter {
		private LinkedList<Mail> mails;
		private LayoutInflater inflater;

		public MailAdapter() {
			mails = new LinkedList<Mail>();
			inflater = LayoutInflater.from(context);
		}

		public MailAdapter(LinkedList<Mail> mails) {
			this.mails = mails;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mails.size();
		}

		@Override
		public Object getItem(int position) {
			return mails.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public void setItems(LinkedList<Mail> mails) {
			this.mails = mails;
		}

		public void add(Mail mail) {
			mails.add(mail);
		}

		public void add(LinkedList<Mail> mails) {
			this.mails = mails;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			Holder holder;
			final Mail info = (Mail) getItem(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_mails, null);
				holder = new Holder();

				holder.contacto = (TextView) convertView
						.findViewById(R.id.txtvMailContacto);
				holder.mailContacto = (TextView) convertView
						.findViewById(R.id.txtvMailContactoMail);
				holder.mailEmisor = (TextView) convertView
						.findViewById(R.id.txtvMailEmisorMail);
				holder.fecha = (TextView) convertView
						.findViewById(R.id.txtvMailFecha);

				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}

			holder.contacto.setText(info.getContacto());
			holder.mailContacto.setText(info.getMailContacto());
			holder.mailEmisor.setText(info.getMailEmisor());
			holder.fecha.setText(info.getFecha().toLocaleString());

			return convertView;
		}

		class Holder {
			private TextView contacto;
			private TextView mailContacto;
			private TextView mailEmisor;
			private TextView fecha;
		}
	}

}
